package com.kitten.framework.context.inteceptor;

import com.kitten.framework.common.constants.GlobalConstants;
import com.kitten.framework.context.holder.LoginUserContextHolder;
import feign.RequestInterceptor;
import feign.RequestTemplate;
import lombok.extern.slf4j.Slf4j;

import java.util.Objects;

@Slf4j
public class FeignRequestInterceptor implements RequestInterceptor {

    @Override
    public void apply(RequestTemplate requestTemplate) {
        // 获取上下文的 user ID
        Long userId = LoginUserContextHolder.getUserId();
        log.info("FeignRequestInterceptor add userId to header, userId: {}", userId);

        // 添加到请求头
        if (Objects.nonNull(userId)) {
            requestTemplate.header(GlobalConstants.USER_ID, String.valueOf(userId));
        }
    }
}
